/*
 * Copyright (c) 2010 Christoc.com
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 
 * software and associated documentation files (the "Software"), to deal in the Software
 * without restriction, including without limitation the rights to use, copy, modify, 
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to the following 
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies 
 * or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */


namespace Com.Christoc.HomeController
{
    using System;
    using DotNetNuke.Services.Exceptions;

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ViewHomeController class displays the content
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class ViewHomeController : HomeControllerModuleBase
    {

        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            Load += Page_Load;
            ControlLoader();
        }


        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if(IsEditable)
                {
                    AdminPanel.Visible = true;
                    DeviceTypeListLink.NavigateUrl = BuildAdminLink("devicetypes");
                    RoomListLink.NavigateUrl = BuildAdminLink("rooms");
                    DeviceListLink.NavigateUrl = BuildAdminLink("devices");
                    
                }
                else
                {
                    AdminPanel.Visible = false;
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

        #region HomeController

        private void ControlLoader()
        {

            try
            {
                //todo: add other control options in here
                string controlToLoad = "Controls/default.ascx";

                var adminType = Request.QueryString["adminType"];
                if(!string.IsNullOrEmpty(adminType))
                {
                    if(adminType=="rooms")
                    {
                        controlToLoad = "admin/RoomList.ascx";
                    }
                    else if (adminType =="devicetypes")
                    {
                        controlToLoad = "admin/DeviceTypesList.ascx";
                    }
                    else if (adminType == "devices")
                    {
                        controlToLoad = "admin/DeviceList.ascx";
                    }
                }

                var mb = (HomeControllerModuleBase)LoadControl(controlToLoad);
                mb.ModuleConfiguration = ModuleConfiguration;
                mb.ID = System.IO.Path.GetFileNameWithoutExtension(controlToLoad);
                LoaderPlaceHolder.Controls.Add(mb);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }

        }

        #endregion

        private string BuildAdminLink(string adminText)
        {
            string resultUrl = DotNetNuke.Common.Globals.NavigateURL(TabId, "", "&adminType=" + adminText);
            return resultUrl;
        }

        #region Optional Interfaces

        //public ModuleActionCollection ModuleActions
        //    {
        //        get
        //        {
        //            ModuleActionCollection Actions = new ModuleActionCollection();
        //            Actions.Add(GetNextActionID(), Localization.GetString("Administration", this.LocalResourceFile), "", "", "", EditUrl(Utility.AdminContainer), false, SecurityAccessLevel.Edit, true, false);
        //            return Actions;
        //        }
        //    }

        #endregion

    }

}
